
var KZ = {};

const isApp = function () {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.indexOf("shopzhipin") > -1) {
        return true;
    } else {
        return false;
    }
}

KZ.isApp = isApp;

const isIos = function () {
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        return true
    } else {
        return false
    }
}

KZ.isIos = isIos;

//图片上传
KZ.onUploadPhotoComplete = function (opt) {
    var _self = opt;
    _self.dialogShowPic = escape(opt.dialogShowPic)
    if (isApp()) {
        if (this.cut) {
            if (isIos()) {
                try {
                    window.webkit.messageHandlers.execute.postMessage("/?cropscale=" + _self.minHeight / _self.minWidth + "&maxscale=" + _self.minWidth + "_" + _self.minHeight + "&album=" + _self.album + "&dialogShowPic=" + _self.dialogShowPic + "#cropphoto")
                } catch (er) {
                    window.location.href = "/?cropscale=" + _self.minHeight / _self.minWidth + "&maxscale=" + _self.minWidth + "_" + _self.minHeight + "&album=" + _self.album + "&dialogShowPic=" + _self.dialogShowPic + "#cropphoto"
                }
            } else {
                try {
                    window.wst.execute("/?cropscale=" + _self.minHeight / _self.minWidth + "&maxscale=" + _self.minWidth + "_" + _self.minHeight + "&album=" + _self.album + "&dialogShowPic=" + _self.dialogShowPic + "#cropphoto");
                } catch (er) { }
            }
        } else {
            if (isIos()) {
                try {
                    window.webkit.messageHandlers.execute.postMessage("/?album=" + _self.album + "&showScale=" + _self.showScale + "&dialogShowPic=" + _self.dialogShowPic + "#cropphoto");
                } catch (er) {
                    window.location.href = "/?album=" + _self.album + "&showScale=" + _self.showScale + "&dialogShowPic=" + _self.dialogShowPic + "#cropphoto"
                }
            } else {
                try {
                    window.wst.execute("/?album=" + _self.album + "&showScale=" + _self.showScale + "&dialogShowPic=" + _self.dialogShowPic + "#cropphoto");
                } catch (er) { }
            }
        }
        window.uploadLoading = function () {
            _self.loading()
        }
        window.uploadPhotoComplete = function (json) {
            //安卓
            if (!isIos()) {
                _self.loading()
            }
            if (json.url) {
                _self.success(json)
            } else {
                _self.fail(json.message)
            }
        }
    }
}

//图片预览
KZ.onPreviewPhoto = function (opt) {

    var _self = opt;
    if (isApp()) {
        if (isIos()) {
            try {
                window.webkit.messageHandlers.execute.postMessage("#viewPic")
            } catch (er) {
                window.location.href = "#viewPic"
            }
        } else {
            try {
                window.wst.execute("#viewPic");
            } catch (er) { }
        }
        window.viewPictures = function (picList) {
            var picList = _self.picList
            picList = JSON.stringify(picList);
            try {
                window.wst.setPictures(picList);
                return;
            } catch (er) { }
            return picList
        }
    }
}

//图片下载到本地
KZ.onDownloadPhoto = function (opt) {

    var _self = opt;
    if (isApp()) {
        if (isIos()) {
            try {
                window.webkit.messageHandlers.execute.postMessage("#download")
            } catch (er) {
                window.location.href = "#download"
            }
        } else {
            try {
                window.wst.execute("#download");
            } catch (er) { }
        }
        window.downloadPhoto = function () {
            //type=1 普通 type=2 base64
            var base64 = _self.img;
            var type = _self.type;
            var url = JSON.stringify(base64.replace(/data\:image\/jpeg\;base64,/, ""));
            var json = JSON.stringify({
                url: url,
                type: type
            })
            try {
                window.wst.startDownload(json)

            } catch (er) { }
            return {
                url: url,
                type: type
            }
        }
    }
}

//关闭webview
KZ.onCloseView = function (opt) {
        if (isIos()) {
            try {
                window.webkit.messageHandlers.execute.postMessage("#closeview")
            } catch (er) {
                window.location.href = "#closeview"
            }
        } else {
            try {
                window.wst.execute("#closeview");
            } catch (er) { }
        }
}

//微信分享
KZ.onWebShare = function (opt) {
    window.getShareMessage = function () {
        var shareMessage = {};
        shareMessage['wxShareTitle'] = opt.wxShareTitle
        shareMessage['wxShareDesc'] = opt.wxShareDesc
        shareMessage['wbShareTitle'] = getSubShareContent(139, opt.wbShareTitle + "戳这里：链接", opt.shortUrl);
        shareMessage['smsShareTitle'] = opt.smsShareTitle + "戳这里：链接" + opt.shortUrl;
        shareMessage['imgUrl'] = opt.imgUrl;
        shareMessage['shareUrl'] = opt.shareUrl;
        shareMessage['subtype'] = 99;
        shareMessage['p'] = 0;
        //window.document.title = shareMessage['wxShareTitle'];
        try {
            if (window.get_share_datas_from_html_inapp) {
                ret = get_share_datas_from_html_inapp();
                for (key in ret) {
                    shareMessage[key] = ret[key];
                }
            }
        } catch (err) { }
        var result = JSON.stringify(shareMessage);
        if (!isIos()) {
            try {
                window.wst.doShare(result);
                return;
            } catch (err) {
                window.wst.startFunction(result);
            }
        } else {
            return result;
        }
    }
    if (isApp()) {
        if (isIos()) {
            try {
                window.webkit.messageHandlers.execute.postMessage("#share")
            } catch (er) {
                window.location.href = "#share"
            }
        } else {
            try {
                getShareMessage()
            } catch (er) { }
        }
    }
}

window.isInterceptColseEvent = function () {

}

window.getShareMessage = function () {

}

window.onShareComplete = KZ.onShareComplete = function (type) {

}

window.canShare = KZ.canShare = function () {
    try {
        window.wst.needShowShare(false);
        return;
    } catch (err) { }
    return false;
}

window.getSubShareContent = KZ.getSubShareContent = function (charNum, content, url) {
    var left = charNum - url.length;
    return content.substring(0, left) + " " + url;
}

//刷新用户状态
KZ.onRefreshSafeStatus = function (opt) {
    window.location.href = "#refreshSafeStatus"
}

//告诉客户端上层刷新
KZ.onRefreshsource = function (opt) {
    window.location.href = "#refreshsource"
}

//退出登录
KZ.onLogout = function (opt) {
    window.location.href = "#logout"
}

//是否开启GPS
KZ.onLogout = function (opt) {
    window.location.href = "#locationJudge"
    window.locationStatus = function (state) { }
}

//点击返回关闭、物理返回 是否能关闭页面(0:关闭,1:不关闭)
KZ.backEvent = function (opt) {
    try {
        window.wst.needBackControl(opt.close)
    } catch (err) { }
    window.isInterceptColseEvent = function (state) {
        var close = opt.close;

        if (typeof (opt.success) == "function") {

            opt.success(state)
        }

        return (close == true) ? true : false

    }
}

KZ.colseEvent = function (opt) {
    var close = false;
    try {
        window.wst.needBackControl(close)
    } catch (err) { }
    if (isIos()) {
        KZ.backEvent({
            close: close,
            success: function () { }
        })
    }
}

//是否安装支付宝
KZ.isAliPay = function (opt) {
    if (isIos()) {
        window.webkit.messageHandlers.execute.postMessage("#getIsAliPayInstall")
        window.getIsAliPayInstall = function (val) {
            opt.success(val)
        }
    } else {
        try {
            opt.success(window.wst.getIsAliPayInstall())
        } catch (err) { }
    }
}

//微信支付
KZ.weixinPay = function (opt) {

    window.location.href = opt.url;
    window.getweixinPay = function (val) {
        opt.success(val)

    }
}

//跳转新的Native页
KZ.jumpNative = function (opt) {

    if (isApp()) {
        if (isIos()) {
            try {
                window.webkit.messageHandlers.execute.postMessage(opt.url)
            } catch (er) {
                window.location.href = opt.url
            }
        } else {
            window.location.href = opt.url
        }
    }
}

//跳转新的Native页
KZ.Toast = function (text) {
    if (isApp()) {
        if (isIos()) {
            window.webkit.messageHandlers.toast.postMessage(text)
        } else {
            window.wst.toast(text)

        }
    }
}

//自定义头部
KZ.setNavigationBar = function (json) {
    if (isApp()) {
        if (isIos()) {
            window.webkit.messageHandlers.updateNavigationBar.postMessage(JSON.stringify(json))
        } else {
            window.wst.updateNavigationBar(JSON.stringify(json))
        }
    }
}

// KZ.setNavigationBar = function (json) {
//     if (isApp()) {
//         if (isIos()) {
//             window.webkit.messageHandlers.updateNavigationBar.postMessage(JSON.stringify(json))
//         } else {
//             window.wst.updateNavigationBar(JSON.stringify(json))
//         }
//     }
// }

//快捷设置导航头
KZ.setNavigationBarShortcut = function (opts) {
    let data = Object.assign({
        "background": "ff5151",
        "color": "ffffff",
        "text": "",
        "link": ""
    }, opts)

    //设置导航颜色
    try {
        KZ.setNavigationBar(data)
    } catch (err) {

    }
}

export default KZ;
